package eu.telecombretagne.rsm.base.entity;

import eu.telecombretagne.rsm.base.event.*;
import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.DesktopFrame;


public class AppEmitterEntity extends Entity {

	static private Class<?> [] producedEvents = {AppMessageEvent.class};
	static private Class<?> [] consumedEvents = {AppMessageReturnEvent.class};
	
	public AppEmitterEntity (EventChannel ec) {
		super (ec, producedEvents, consumedEvents);
	}
	
	public void run () {
		int i = 0;
		
		while(true){
			try {
				Thread.sleep (1000);
			}
			catch (InterruptedException ie) {
				DesktopFrame.writeLogError("AppEmitterEntity" + ie);
				System.exit(1);
			}
			AppMessageEvent ame = new AppMessageEvent ("Applicative message: " + i++);
			
			DesktopFrame.writeLogEmitter(NameConsts.APPEMITTERENTITYSENDINGAPPLICATIVEMESSAGE + ame.getBody ());
			
			this.produce (ame);
			try {
				Event e = this.consume ();
				if (e.getClass () == AppMessageReturnEvent.class) {
					DesktopFrame.writeLogEmitter(NameConsts.APPEMITTERENTITYRECEIVINGACKFORAPPLICATIVEMESSAGE);
				} else {
					DesktopFrame.writeLogEmitter(NameConsts.BUGAPPEMITTERENTITYRECEIVING + e.getClass ());
				}	
			}
			catch (InterruptedException ie) {
				DesktopFrame.writeLogError("AppEmitterEntity" + ie);
				System.exit(1);
			}
		}
	}
}